// pages/pm101/pm101.js
const App = getApp()

let { AJAX, timeTranfer,imgUrl} = require("../../utils/util.js")
Page({
   /**
    * 页面的初始数据
    */
   data: {
      navH: '',
      detail: [],
      videoList: [],
      play: 0,
      love: 0,
      look: 0,
      lookID: 0,
     imgUrl: imgUrl
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      this.setData({
         navH: App.globalData.navHeight
      })
      let id = options.id;
      if (!wx.getStorageSync("ACCESSTOKEN")) {
         App.userInfoReadyCallback = res => {
            wx.setStorageSync('ACCESSTOKEN', res);
            let id = options.id;
            this.requestDetail(id)
            this.requestList(id)
         }
         return
      }
      this.requestDetail(id)
      this.requestList(id)
      
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
     
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },
   /**
    * 喜欢
    * love
    * idea/love
    */
   love: function(e) {
      console.log(e.currentTarget.dataset.id)
      let vm = this
      AJAX("idea/love", "GET", {
         id: e.currentTarget.dataset.id
      }).then(res => {
         console.log(res.data.is_love)
         console.log(res.data.love)
         this.setData({
            ['detail.is_love']: res.data.is_love,
            ['detail.love']: res.data.love
         })

      })
   },
   /**
    * 列表
    * idea/list
    */
   requestList: function(id) {
      let vm = this
      AJAX("idea/list", 'GET', {
         id: id
      }).then(res => {

         res.data.forEach(function(item, index, arr) {
            item.created_at = timeTranfer(item.created_at, 1)
         })

         vm.setData({
            videoList: res.data
         })
      })
   },




   /**
    * 点击某一个
    */
   clickitem: function(e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
         url: '/pages/pm101/pm101?id=' + id,
      })
      this.requestDetail(id)
   },

   /***
    * 详情
    */
   requestDetail: function(id) {
      let vm = this
      if (!id) {
         AJAX("idea/first", "GET").then(res => {
           // console.log(res.data)
            if (res.data) {
               res.data.created_at = timeTranfer(res.data.created_at, 1);
            }
            vm.setData({
               detail: res.data,
               lookID: res.data.id,
               look: res.data.look
            })

         })
         AJAX("idea/look", 'GET', {
            id: wx.getStorageSync("id")
         }).then(res => {
            //console.log(res.data)
         })        

      } else {
         AJAX("idea/detail", "GET", {
            id: id
         }).then(res => {            
            if (res.data) {
               res.data.created_at = timeTranfer(res.data.created_at, 1);
            }
            vm.setData({
               detail: res.data,
               love: res.data.is_love,
               lookID: res.data.id,
               look: res.data.look
            })
         })
         AJAX("idea/look", 'GET', {
            id:id
         }).then(res => {
            console.log(res.data)
         })
      }
   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },
   navBack() {
      wx.switchTab({
         url: '/pages/index/index'
      })
   }
})